package com.hxxy.wyp.petsLover.common.services.base.backend.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxxy.wyp.petsLover.common.entities.User;
import com.hxxy.wyp.petsLover.common.entities.UserShare;
import com.hxxy.wyp.petsLover.common.entities.vo.req.HomeInfoReq;
import com.hxxy.wyp.petsLover.common.entities.vo.req.SearchObj;
import com.hxxy.wyp.petsLover.common.mapper.UserMapper;
import com.hxxy.wyp.petsLover.common.mapper.UserShareMapper;
import com.hxxy.wyp.petsLover.common.services.base.backend.UserShareService;
import com.hxxy.wyp.petsLover.security.SecurityContextFrontUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName UserShareServiceImpl
 * @Description TODO
 * @Author wyp
 * @Date 2023/2/1 16:02
 * @Version 1.0
 */
@Service
public class UserShareServiceImpl extends ServiceImpl<UserShareMapper, UserShare> implements UserShareService {

    @Resource
    private UserShareMapper userShareMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private SecurityContextFrontUser securityContextFrontUser;

    /**
     * old版
     *
     * @param pageInfo
     * @param lambdaQueryWrapper
     * @return
     */
    @Override
    public Page pageSearch(Page pageInfo, LambdaQueryWrapper<UserShare> lambdaQueryWrapper) {
        return baseMapper.selectPage(pageInfo, lambdaQueryWrapper);
    }

    /**
     * 改良版分页查询
     *
     * @param searchObj
     * @return
     */
    @Override
    public Page<UserShare> pageByUserShare(SearchObj searchObj) {
        Page<UserShare> userShare = new Page<>(Long.parseLong(searchObj.getPage()), Long.parseLong(searchObj.getPageSize()));
        LambdaQueryWrapper<UserShare> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserShare::getStatus, "1");
        lambdaQueryWrapper.orderByDesc(UserShare::getShareTime);
        userShareMapper.selectPage(userShare, lambdaQueryWrapper);
        userShare.getRecords().stream().map((userShare1) -> {
            List<String> shareImgList = Arrays.stream(userShare1.getSharePic().split(",")).collect(Collectors.toList());
            userShare1.setSharePic(shareImgList.get(0));
            userShare1.setSharePicList(shareImgList);
            User user = userMapper.selectById(userShare1.getUserId());
            userShare1.setUser(user);
            return userShare1;
        }).collect(Collectors.toList());
        return userShare;
    }

    @Override
    public boolean userShareTheir(UserShare userShare) {
        User userInfo = securityContextFrontUser.getUserInfo();
        userShare.setUserId(userInfo.getId());
        userShare.setShareName(userInfo.getNickname());
        List<HomeInfoReq> fileList = userShare.getFileList();
        String s = fileList.stream().map(HomeInfoReq::getUrl).collect(Collectors.toList()).toString();
        String substring = s.substring(1, s.length() - 1);
        userShare.setSharePic(substring);
        int insert = userShareMapper.insert(userShare);
        return insert > 0;
    }
}
